<?php
/**
 * Created by 2019/4/13 0013
 * Effect: TokenValidationMiddleware.php
 * Author: 品花
 * Date: 2019/4/13 0013
 * Time: 上午 10:37
 */

namespace Middleware;


use App\models\FdManager;
use Consts\RedisKey;
use Helper\Config;
use Helper\Output;
use Helper\Mysql;
use Helper\Redis;
use Helper\Reg;

class AuthMiddleware
{
    protected static $instance;

    public static function getInstance()
    {
        if ( !isset(self::$instance)) {
            self::$instance = new static();
        }
        return self::$instance;
    }

    public function getMenus($role_id){

        if($role_id == 1){
            //超级管理员需要排除不需要的菜单
            $menus = Mysql::getInstance()
                ->where('state',1)
                ->where('super_manager_show',1 )
                ->orderBy('order_','DESC')
                ->orderBy('id')
                ->get('authority');
        }else{
            $ids =  Mysql::getInstance()->where('id',$role_id)->getValue('role','authority_ids');

            $menus = Mysql::getInstance()
                ->where('state',1)
                ->where("id",explode(',',$ids),'in')
                ->where('pid',-1 ,'>')
                ->orderBy('order_','DESC')
                ->orderBy('id')
                ->get('authority');


        }
        if(!$menus){
            return [];
        }
        $menus =  make_tree($menus,'id','pid','child');

        return  $menus;

    }

    /**
     * 获取管理员菜单列表
     * @param $role_id
     * @return array
     * @throws \Exception
     */
    public function getAuthority($role_id){


        if($role_id == 1){
            $menus = Mysql::getInstance()->get('authority');
        }else{
            $ids =  Mysql::getInstance()->where('id',$role_id)->getValue('role','authority_ids');
            $ids = explode(',',$ids);
            $ids[] = 22 ;
            $ids[] = 41 ;
            $menus = Mysql::getInstance()->where("id",$ids,'in')->get('authority');
        }
        if($menus){
            return   $routes = array_column($menus,'route');

        }

        return  [];
    }




    /**
     * 验证后台访问权限
     * @param $request
     * @param $response
     * @return bool
     */
    public function menuAuth($request, $response)
    {
        $uri = $request->server['request_uri'];

        $routes = Redis::getInstance()->get(RedisKey::user_authority_list(Reg::getInstance()->get('user_id')));
        if($routes){
            $routes = json_decode($routes,true);
        }else{
            $routes = [];
        }
        $passRoute = Config::getInstance()->get('pass_route');
        $routes = array_merge($routes,$passRoute);

        if (!$routes) {
            Output::responseJson(403);
            return false;
        }

        if(!in_array($uri,$routes)){
            Output::responseJson(401);
            return false;
        }
        if(!in_array($uri,$passRoute) && Reg::getInstance()->get('user')->role_id != 1){
            //ws是否在线
            $fd = FdManager::getFdByAuid(Reg::getInstance()->get('user_id'));
            if(!$fd){
                Output::responseJson(1026);
                return false;
            }

        }

        Reg::getInstance()->set('role',2);
        return true;

    }


    /**
     * 验证api
     * @param $request
     * @param $response
     * @return bool
     */
    public function apiAuth($request, $response)
    {
        $uri = $request->server['request_uri'];
        $passRoute = Config::getInstance()->get('pass_route');
        if(!in_array($uri,$passRoute)){

            //ws是否在线
            $uid = Reg::getInstance()->get('customer_id');
            $fd  = FdManager::getFdByUid($uid);
            if(!$fd){
                Output::responseJson(1026);
                return false;
            }
        }

        Reg::getInstance()->set('role',1);

        return true;


    }



}

